<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}

class DeliveryClass {
	var $db;
	var $table;
	var $tablepre;
	function __construct(){
		//设置全局变量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."delivery";
	}
	function DeliveryClass() {
		$this->__construct();
	}
	function add($sarray){
		if(is_array($sarray)){
			if(!empty($sarray['gid'])){
				//插入数据库操作	
				if(is_numeric(inserttable($this->table, $sarray,1)))return "OK";
			}return "缺少参数";
		}return "数据格式错误";
		
	}
	function edit($did,$sarray){
		if(is_array($sarray) && !empty($did)){
			if(!empty($sarray['gid'])){
				//更新数据库操作
				$wheresqlarr=" did='{$did}'";
				updatetable($this->table,$sarray, $wheresqlarr);
				return "OK";
			}return "缺少参数";
		}return "参数错误";
	}	
	function del($did){
		if(is_numeric($did))
		{
			$this->db->query("delete from {$this->table} where did={$did}");
			return "OK";
			
		}else {
			return "参数错误";			
		}
	}
	function getone($did){
		if (GetNum($did)) {
			$gpdata=$this->getdata(1,"did='{$did}'");
			$value=$gpdata[0];
		}
		
		return $value;
	}
	
	/**
	 * 获取数据数组
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby=""){
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by did desc";
		$sql="select * from {$this->table}{$where}{$orderby}{$limit}";
		$query =$this->db->query($sql);
		while($value = $this->db->fetch_array($query)) {
			$tempdata[]=$value;
		}
		return $tempdata;
	
	}
	
	function getcount($where=""){
	if(!empty($where))$where=" where $where";
	$count= $this->db->result_first("Select count(*) From `$this->table` $where");
	return $count;
	
	}
	
	function getHtml($did){
		$data = $this->getone($did);
	}
	

	
	//取得運費計算方法類型
	function d_type_array(){
		$d_type = array();
		$d_type[1] = '數量區間, X  < 數量 < Y' ;
		$d_type[2] = '數量小於等於, 數量 <= ' ;
		$d_type[3] = '數量大於等於, 數量 >= ' ;
		$d_type[4] = '數量倍數, ex,每購買3 個算一次運費 。' ;
		$d_type[5] = '價格區間, X < 價格 < Y' ;
		$d_type[6] = '價格小於等於, 價格 <=' ;
		$d_type[7] = '價格大於等於, 價格 >= ' ;
		$d_type[8] = '價格倍數, ex,每購買1000元 算一次運費 。' ;
		return $d_type;	
	}
	
	//計算運費規則 符合規則 回傳運費
	function d_type_rule($num=0,$price=0 ,$type,$d_fee,$d_num_from = 0, $d_num_to = 0){
		if(!empty($type) && $type == 1){
			if($d_num_from <  $num  && $d_num_to > $num){return $d_fee;}else{return false;} 
		}else if(!empty($type) && $type == 2){
			if($num <= $d_num_from){return $d_fee;}else{return 0;} 			
		}else if(!empty($type) && $type == 3){
			if($num >= $d_num_from){return $d_fee;}else{return 0;} 			
		}else if(!empty($type) && $type == 4){
			return ceil($num / $d_num_from ) * $d_fee; 			
		}else if(!empty($type) && $type == 5){
			if($d_num_from <  $price  && $d_num_to > $price){return $d_fee;}else{return false;} 
		}else if(!empty($type) && $type == 6){
			if($price <= $d_num_from){return $d_fee;}else{return 0;} 			
		}else if(!empty($type) && $type == 7){
			if($price >= $d_num_from){return $d_fee;}else{return 0;} 			
		}else if(!empty($type) && $type == 8){
			return ceil($price / $d_num_from ) * $d_fee; 			
		}
	}

	function get_d_type_name($val){
		$d_type = $this->d_type_array();
		return $d_type[$val];
	}
	
	function get_d_num_from_to_text($type , $from , $to){
		if($type == 1){
			return $from." < 數量 < ".$to;
		}else if($type == 2){
			return "數量 <= ".$from;
		}else if($type == 3){
			return "數量 >= ".$from;
		}else if($type == 4){
			return $from." 倍數";
		}else if($type == 5){
			return $from." < 數量 < ".$to;
		}else if($type == 6){
			return "數量 <= ".$from;
		}else if($type == 7){
			return "數量 >= ".$from;
		}else if($type == 8){
			return $from." 倍數";
		}
	}
	
	
	function getHtmlSelectType($val = 1 , $other = ""){
		include_once '../common.inc.php';
		return getselectstr("d_type",$this->d_type_array() , $val ,$other );
	}
	
	/*
	 * 檢查運費
	 */
	function checkFee($gid,$num=0,$price=0){
		$feeArray = $this->getdata('','gid = '.$gid);
		foreach($feeArray as $val){
			$temp_fee = $this->d_type_rule($num,$price,$val['d_type'],$val['d_fee'],$val['d_num_from'],$val['d_num_to'] );
			if($temp_fee > $fee){
				$fee = $temp_fee;
			}
		}
		return $fee;
	}
	
	
}
?>